//可以编辑的项目bug列表grid
var testProjectBugGrid = Ext.create('Ext.grid.Panel', {
    store: testProjectBugGridStore,
    title: '测试BUG',
    columns: [new Ext.grid.RowNumberer(), // 显示行号
        {
            text: '名称',
            flex: 1,
            dataIndex: 'title',
            sortable: false
        }, {
            text: '提交人',
            dataIndex: 'authorName',
            flex: 1,
            sortable: false
        }, {
            text: '更新时间',
            dataIndex: 'updateDate',
            flex: 1,
            sortable: false,
            renderer: function (v, m, r) {
                return v == null || v == '' ? '' : v.replace('T', '  ');//使用reset()会分别经历null和空字符串
            }
        }, {
            text: '操作',
            align: 'center',
            sortable: false,
            xtype: 'actioncolumn',
            width: 90,
            items: [{
                icon: 'static/images/icons/fam/look.ico',
                tooltip: '查看详情',
                handler: function (grid, rowIndex, colIndex, actionItem, event, record, row) {
                    testProjectBugSeeDetailClick(record);
                }
            }]
        }
    ],
    selModel: Ext.create('Ext.selection.CheckboxModel', {
        listeners: {
            selectionchange: function (sm, selections) {
                testProjectBugGrid.down('#deleteButton').setDisabled(selections.length === 0);
            }
        }
    }),
    dockedItems: [{
        xtype: 'toolbar',
        items: [{
            text: '添加',
            tooltip: '添加bug',
            itemId: 'addButton',
            icon: 'static/images/icons/fam/add.gif',
            listeners: {
                click: function () {
                    testProjectBugAddClick();
                }
            }
        }, '-', {
            text: '删除',
            tooltip: '删除bug',
            itemId: 'deleteButton',
            icon: 'static/images/icons/fam/delete.gif',
            listeners: {
                click: function () {
                    testProjectBugDeleteClick();
                }
            }
        }]
    }],
    features: [{
        id: 'group',
        ftype: 'groupingsummary',
        groupHeaderTpl: new Ext.XTemplate(
            '对应需求: {[values.name.substring(32)]}'
        ),
        hideGroupedHeader: false,
        enableGroupingMenu: false
    }]
});

//可编辑的显示bug附件列表的Grid
var testProjectBugAttachedGrid = Ext.create('Ext.grid.Panel', {
    store: testProjectBugAttachedGridStore,
    columns: [new Ext.grid.RowNumberer(), // 显示行号
        {
            text: '名称',
            flex: 1,
            dataIndex: 'name',
            sortable: false
        }, {
            text: '操作',
            align: 'center',
            sortable: false,
            xtype: 'actioncolumn',
            width: 90,
            items: [{
                icon: 'static/images/icons/fam/download.ico',
                tooltip: '下载附件',
                handler: function (grid, rowIndex, colIndex, actionItem, event, record, row) {
                    if (record.data.isCreate)
                        Ext.Msg.alert('错误', '新附件未保存无法下载！');
                    else
                        fileDownload(zuulUrl + '/' + testServiceName + '/testProject/bug/attach/' + record.data.id);
                }
            }]
        }
    ],
    selModel: Ext.create('Ext.selection.CheckboxModel', {
        listeners: {
            selectionchange: function (sm, selections) {
                testProjectBugAttachedGrid.down('#deleteButton').setDisabled(selections.length === 0);
            }
        }
    }),
    dockedItems: [{
        xtype: 'toolbar',
        items: [{
            text: '添加',
            tooltip: '添加附件',
            itemId: 'addButton',
            icon: 'static/images/icons/fam/add.gif',
            listeners: {
                click: function () {
                    document.getElementById('bugAttaInput').click();
                }
            }
        }, '-', {
            text: '删除',
            tooltip: '删除附件',
            itemId: 'deleteButton',
            icon: 'static/images/icons/fam/delete.gif',
            listeners: {
                click: function () {
                    var item = testProjectBugAttachedGrid.getSelectionModel().getSelected();// 得到被选择的行的数组.
                    if (item.length == 0)
                        Ext.Msg.alert('错误', '请选择要删除的附件！');
                    else {
                        Ext.MessageBox.confirm('删除', '你确定要将这些附件删除吗', function (btn) {
                            if (btn === 'yes') {
                                for (var i = 0, length = item.items.length; i < length; i++) {
                                    if (item.items[i].data.isCreate) {//如果是新添加的附件的话
                                        var index = 0;
                                        var data = testProjectBugAttachedGridStore.getData().items;
                                        for (var j = 0, length2 = data.length; j < length2; j++) {//算出它是第几个附件
                                            if (data[j].data.isCreate) {
                                                if (data[j].data.id == item.items[i].data.id)
                                                    break;
                                                index++;
                                            }
                                        }
                                        testProjectBugFileArray.splice(index, 1);//然后再把对应储存新上传文件数据的数组对象删除
                                    }
                                    testProjectBugAttachedGridStore.remove(item.items[i]);//不然的话就只要将store的记录清除就好了
                                    i--;
                                    length--;
                                }
                            }
                        });
                    }
                }
            }
        }]
    }]
});
